package com.winderinfo.yikaotianxia.ui.xiangqin;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.AutofitHeightViewPager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class IntroductionFragment extends BaseFragment implements Contract.View {

    @BindView(R.id.in_image)
    ImageView inImage;
    @BindView(R.id.in_name)
    TextView inName;
    @BindView(R.id.in_title)
    TextView inTitle;
    Unbinder unbinder;
    @BindView(R.id.in_web)
    WebView inWeb;
    private Presenter presenter;
    //创建Map
    Map<String, Object> map = new HashMap<>();

    //发现部分图片 的html 标签 没有定义 width 和hight 属性，默认的还是图像大小，造成了还是图片没有适应屏幕。
    //解决思路：找到所有的img 标签把它的宽高属性全部重置定死。
    //添加了一句js 代码。
    String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $img = document.getElementsByTagName('img');for(var p in  $img){$img[p].style.width = '100%'; $img[p].style.height ='auto'}}</script>";


    @Override
    protected int initLayoutId() {
        return R.layout.fragment_intf;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
    }

    @Override
    protected void initData() {
        //师资Id
        int authorid = getArguments().getInt("authorid");
        //请求师资详情
        /**
         * @id (返回商品中的author_id ）
         * */
        map.clear();
        map.put("id", authorid);
        presenter.onPostRequest(Api.SHIZIXIANGQN, map, AboutInstructorBean.class);
    }

    @Override
    public void onSuccess(Object data) {
        //师资详情数据
        if (data instanceof AboutInstructorBean) {
            //设置值
            Glide.with(getActivity())
                    .load(((AboutInstructorBean) data).getYkAuthor().getAuthorPhoto())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(inImage);
            inName.setText(((AboutInstructorBean) data).getYkAuthor().getAuthorName());
            inTitle.setText(((AboutInstructorBean) data).getYkAuthor().getAuthorPosition());
            //解析H5
            //支持javascript
            inWeb.getSettings().setJavaScriptEnabled(true);
            String authorDetails = ((AboutInstructorBean) data).getYkAuthor().getAuthorDetails();
            inWeb.loadDataWithBaseURL("", varjs + authorDetails, "text/html", "UTF-8", null);
        }

    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
